#!/bin/bash
echo "Fiber tracking by DSI Studio"

START=$(date +%s)
echo "Started at: $START"

DATA=$1

if [ -z "$DATA" ]; 
	then echo "Warning: the first folder will be used as source";
	for DIR in $PWD/*/; 
	do 
	    echo "DIR: $DIR"; 
	    DATA=$DIR;
	    break;
	done
else 
	echo "DATA: $DATA"
fi

if [ -z "$DATA" ]; 
	then echo "Please specify the source folder";
else 
	echo "DICOM: $DATA"
		
	rm -rf $PWD/data
		
	mkdir -p $PWD/data
	mkdir -p $PWD/data/DICOM
		
	cp -R "$DATA/." $PWD/data/DICOM
		
	rm -rf $PWD/fiber-tracking
		
	git clone https://gitee.com/ecsoya/fiber-tracking.git
		
	cp -R $PWD/fiber-tracking/dsi_studio/scripts $PWD/data
		
	mkdir -p $PWD/data/logs
	mkdir -p $PWD/data/fib
	mkdir -p $PWD/data/target
	mkdir -p $PWD/data/src
	mkdir -p $PWD/data/trk
		
	rm -rf $PWD/fiber-tracking
	
	echo "Fiber tracking..."
	docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $PWD/data:/data dsistudio/dsistudio:latest /bin/bash /data/scripts/run.all.sh > run.all.log
fi

END=$(date +%s)    
echo "Finished at $END"
DIFF=$(echo "$END - $START" | bc)

echo "Totally used: $DIFF s"